package com.hockeo.shared.domain.transitory;

import com.hockeo.shared.domain.AbstractDomain;
import com.hockeo.shared.domain.persistent.Manager;

/**
 * Represents information about the current user session (e.g. who is logged in etc.).
 *
 * @version $Id$
 * @author jjanke
 */
public class SessionInfo extends AbstractDomain<SessionInfo, String>
{
  /** Connected manager or <code>null</code> if nobody is logged in. */
  private Manager d_connectedManager;

  /** Indicates if the connected user is an admin or not. */
  private boolean d_fAdmin;

  /** Login or logout URL, depending on whether a user is currently connected or not. */
  private String  d_strLogInOutUrl;

  /** Argument-less constructor for GWT/RPC. */
  private SessionInfo()
  {}

  /**
   * Creates a new SessionInfo object.
   *
   * @param manager the currently connected manager or <code>null</code>
   * @param strUrl the login URL if no user is connected or the logout URL if a user is
   *          connected
   */
  public SessionInfo( Manager manager, String strUrl )
  {
    d_connectedManager = manager;
    d_strLogInOutUrl = strUrl;
  }

  /**
   * Returns the currently connected manager or <code>null</code> if nobody is connected.
   */
  public Manager getConnectedManager()
  {
    return d_connectedManager;
  }

  /**
   * Returns the URL of the login page if nobody is connected or the URL of the logout
   * page if someone is connected.
   */
  public String getLogInOutUrl()
  {
    return d_strLogInOutUrl;
  }

  /**
   * Returns <code>true</code> if a manager is connected, otherwise <code>false</code>.
   */
  public boolean isManagerConnected()
  {
    return d_connectedManager != null;
  }

  /**
   * Returns <code>true</code> if there is a user connected and he is recognized as being
   * an admin.
   */
  public boolean isManagerAdmin()
  {
    return d_connectedManager != null && d_fAdmin;
  }

  /**
   * Marks/unmarks the currently connected manager as admin.
   *
   * @param flag indicates whether the connected manager is an admin or not
   */
  public void setManagerAdmin( boolean flag )
  {
    if ( !isManagerConnected() )
      throw new IllegalStateException( "No manager that can be marked as admin is connected." );

    d_fAdmin = flag;
  }

  @Override
  public String getEntityName()
  {
    return "SessionInfo";
  }

  @Override
  public String getId()
  {
    return d_connectedManager == null ? "NoConnectedManager" : d_connectedManager.getId();
  }
}
